import { Head } from "$fresh/runtime.ts"; import { Handlers, PageProps } from "$fresh/server.ts"; import { checkToken } from "utils/server.ts"; import { find } from "utils/db.ts"; import TopBar from "../islands/TopBar.tsx"; import Editor, { EditorMode } from "../islands/Editor.tsx"; interface PostProps { id: string; title: string; content: string; shared: boolean; isLogined: boolean; allowMode: EditorMode; } export const handler: Handlers = { GET(req, ctx) { const tokenUserId = checkToken(req); const postId = ctx.params.id; const post = find( "Post", tokenUserId ? { id: postId, user_id: tokenUserId, } : { id: postId, shared: true }, ["title", "content", "shared"], ); if (post.length > 0) { return ctx.render({ id: postId, isLogined: Boolean(tokenUserId), allowMode: tokenUserId ? EditorMode.Both : EditorMode.Read, title: post[0][0] as string, content: post[0][1] as string, shared: post[0][2] as boolean, }); } // Redirect to 404 page if not found return ctx.renderNotFound(); }, }; export default function Post(props: PageProps) { return ( <> {props.data.title}
); }